/**
 * 音乐频谱
 * TODO 现在很多波形会画出框，需要调整
 * ibio-develop
 * 2008-11-22 17:42
 */
package com.ibio8.components.wave {
	import com.ibio8.utils.MathUtil;
	import com.ibio8.components.wave.behavior.*;
	import com.ibio8.components.wave.WaveBody;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.display.Bitmap;
	import flash.display.BlendMode;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.utils.Timer;
	import flash.utils.ByteArray;
	import flash.media.SoundMixer;
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.BitmapFilterQuality;
	
	public class  SoundWaveFactory extends Sprite {
		public static const TOTAL_NUM:uint = 6;
		public const RANDOM:int = -1;
		
		protected var m_timer:Timer;
		protected var m_waveBody:WaveBody;
		protected var m_bArray:ByteArray;
		protected var m_bmpData:BitmapData;
		protected var m_waveBehavior:IWaveBehavior;
		protected var m_blur:BlurFilter = new BlurFilter(10, 10, BitmapFilterQuality.LOW);
		protected var m_colorMatrix:ColorMatrixFilter = new ColorMatrixFilter([
            0.98,0,0,0,0,
            0,0.98,0,0,0,
            0,0,0.98,0,0,
            0,0,0,0.90,0,
        ]);
		
		/**
		 * 构造函数
		 * @param	waveBox	需要呈现波形的容器
		 */
		public function SoundWaveFactory(waveBox:Sprite) {
			//位图背景
			m_bmpData = new BitmapData(waveBox.width, waveBox.height, true, 0x000000);
			var bmp:Bitmap = new Bitmap(m_bmpData);
			//混合模式为"添加"
			waveBox.blendMode = BlendMode.ADD;
			waveBox.addChild(bmp);
			//波形sprite
			m_waveBody = new WaveBody();
			waveBox.addChild(m_waveBody);
			m_waveBody.setSize(waveBox.width, waveBox.height);
			
			m_bArray = new ByteArray();
			
			m_timer = new Timer(50);
			m_timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
		}
		
		/**
		 * 画出波形
		 * @param	effect	0 - TOTAL_NUM 不同的波形
		 */
		public function createWave(effect:int = 0):void {
			if (!m_timer.running) {
				m_timer.start();
			}
			var num:int = effect;
			if (num == RANDOM) {
				num = MathUtil.random(0, TOTAL_NUM);
			}
			switch (num) {
				case 0:
					m_waveBehavior = new Circle();
					break;
				case 1:
					m_waveBehavior = new DoubleCircle();
					break;
				case 2:
					m_waveBehavior = new DoubleLine();
					break;
				case 3:
					m_waveBehavior = new RotateLine();
					break;
				case 4:
					m_waveBehavior = new DoubleRotateLine();
					break;
				case 5:
					m_waveBehavior = new Line();
					break;
				default:
					m_waveBehavior = new Circle();
			}
		}
		
		public function setLineStyle(lineWidth:Number, lineColor:Number, lineAlpha:Number):void {
			m_waveBody.setLineStyle(lineWidth, lineColor, lineAlpha);
		}
		
		protected function onTimerHandler(e:Event):void {
			m_waveBody.reset();
			SoundMixer.computeSpectrum(m_bArray);
			m_waveBehavior.show(m_bArray, m_waveBody);
			//背景图
			m_bmpData.draw(m_waveBody);
			m_bmpData.applyFilter(m_bmpData, m_bmpData.rect, new Point(), m_blur);
			m_bmpData.applyFilter(m_bmpData, m_bmpData.rect, new Point(), m_colorMatrix);
			//向右移3px，使图像有种飘动的感觉
			m_bmpData.scroll(3, 0);
		}
	}
}